package com.agriculture.monitoring.controller;

import com.agriculture.monitoring.dto.ApiResponse;
import com.agriculture.monitoring.dto.PathPointDTO;
import com.agriculture.monitoring.service.PathPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/path-points")
@CrossOrigin(origins = "*")
public class PathPointController {
    
    @Autowired
    private PathPointService pathPointService;
    
    @GetMapping("/device/{deviceId}")
    public ResponseEntity<ApiResponse<List<PathPointDTO>>> getPointsByDeviceId(
            @PathVariable Long deviceId) {
        try {
            List<PathPointDTO> points = pathPointService.getPointsByDeviceId(deviceId);
            return ResponseEntity.ok(ApiResponse.success("获取轨迹点成功", points));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取轨迹点失败: " + e.getMessage()));
        }
    }
    
    @GetMapping("/path/{pathId}")
    public ResponseEntity<ApiResponse<List<PathPointDTO>>> getPointsByPathId(
            @PathVariable Long pathId) {
        try {
            List<PathPointDTO> points = pathPointService.getPointsByPathId(pathId);
            return ResponseEntity.ok(ApiResponse.success("获取轨迹点成功", points));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取轨迹点失败: " + e.getMessage()));
        }
    }
    
    @GetMapping("/device/{deviceId}/time-range")
    public ResponseEntity<ApiResponse<List<PathPointDTO>>> getPointsByTimeRange(
            @PathVariable Long deviceId,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        try {
            List<PathPointDTO> points = pathPointService.getPointsByDeviceAndTimeRange(
                    deviceId, startTime, endTime);
            return ResponseEntity.ok(ApiResponse.success("获取轨迹点成功", points));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("获取轨迹点失败: " + e.getMessage()));
        }
    }
    
    @PostMapping
    public ResponseEntity<ApiResponse<PathPointDTO>> createPathPoint(
            @RequestBody PathPointDTO pointDTO) {
        try {
            PathPointDTO created = pathPointService.createPathPoint(pointDTO);
            return ResponseEntity.ok(ApiResponse.success("创建轨迹点成功", created));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("创建轨迹点失败: " + e.getMessage()));
        }
    }
    
    @PostMapping("/batch")
    public ResponseEntity<ApiResponse<Void>> batchCreatePathPoints(
            @RequestBody List<PathPointDTO> pointDTOs) {
        try {
            pathPointService.batchCreatePathPoints(pointDTOs);
            return ResponseEntity.ok(ApiResponse.success("批量创建轨迹点成功", null));
        } catch (Exception e) {
            return ResponseEntity.badRequest()
                    .body(ApiResponse.error("批量创建轨迹点失败: " + e.getMessage()));
        }
    }
}